﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using BesAsm.MasterData.Entities;
using BesAsm.MasterData.Services;

namespace BesAsm.Emgaats.Tools
{
  public partial class ManageLayersForm : Form
  {
    TList<LayerFile> _layerFiles;    

    public ManageLayersForm()
    {
      InitializeComponent();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
      try
      {
        SaveChanges();
      }
      catch (Exception ex)
      {
        MessageBox.Show("Unable to save changes: " + ex.Message,
          "Error saving changes!", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
      Close();
    }

    private void btnUpload_Click(object sender, EventArgs e)
    {
      if (openFileDialog1.ShowDialog() != DialogResult.OK)
        return;

      try
      {
        string file = openFileDialog1.FileName;
               
        LayerFile lf = new LayerFile();        
        lf.Name = System.IO.Path.GetFileNameWithoutExtension(file);
        lf.Description = "Enter a description for layer " + lf.Name;
        lf.AppendDate = DateTime.Now;

        lf.ReadLayer(file);
        
        UpdateLayerForm ulf = new UpdateLayerForm(lf);
        if (ulf.ShowDialog() != DialogResult.OK)
          return;

        _layerFiles.Add(ulf.LayerFile);
      }
      catch (Exception ex)
      {
        MessageBox.Show("Unable to upload layer file: " + ex.Message,
          "Error uploading file!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }
      finally
      {
        grdSelectResultsLayers.Rows.Refresh(Infragistics.Win.UltraWinGrid.RefreshRow.ReloadData);

      }
      btnSave.Enabled = true;

    }

    private void btnDownload_Click(object sender, EventArgs e)
    {
      if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
        return;

      try
      {
        string path = folderBrowserDialog1.SelectedPath;

        LayerFile lf;
        lf = (LayerFile)grdSelectResultsLayers.ActiveRow.ListObject;

        lf.WriteLayer(path);
      }
      catch (Exception ex)
      {
        MessageBox.Show("Unable to download layer file: " + ex.Message,
          "Error downloading file!", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }


    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
      if (grdSelectResultsLayers.ActiveRow == null)
      {
        return;
      }

      LayerFile lf = (LayerFile)grdSelectResultsLayers.ActiveRow.ListObject;

      LayerFileService lfs = new LayerFileService();
      lf.MarkToDelete();
      _layerFiles.Remove(lf);
      btnSave.Enabled = true;

      if (grdSelectResultsLayers.ActiveRow == null)
      {
        btnDelete.Enabled = false;
        btnDownload.Enabled = false;
      }
    }

    private void SaveChanges()
    {
      LayerFileService lfs = new LayerFileService();

      if (_layerFiles.IsDirty)
        lfs.Save(_layerFiles);

      btnSave.Enabled = false;
    }

    private void ManageLayersForm_Load(object sender, EventArgs e)
    {
      LayerFileService lfs = new LayerFileService();
      _layerFiles = lfs.GetAll();
      
      layerFileBindingSource.DataSource = _layerFiles;

    }

    private void grdSelectResultsLayers_AfterRowActivate(object sender, EventArgs e)
    {
      if (grdSelectResultsLayers.ActiveRow == null)
      {
        btnDownload.Enabled = false;
        btnDelete.Enabled = false;
      }
      else
      {
        btnDownload.Enabled = true;
        btnDelete.Enabled = true;
        LayerFile lf = (LayerFile)grdSelectResultsLayers.ActiveRow.ListObject;
        txtLayerDescription.Text = lf.Description;
      }
    }

    private void ManageLayersForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (_layerFiles == null)
        return; 

      if (_layerFiles.IsDirty)
      {
        if (MessageBox.Show("Changes to layer files detected. Save to database?",
          "Save changes to database?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
          != DialogResult.No)
        {
          try
          {
            SaveChanges();
          }
          catch (Exception ex)
          {
            MessageBox.Show("Unable to save changes: " + ex.Message,
        "Error saving changes!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            e.Cancel = false;
          }
        }
      }
    }

  }
}
